package com.city.justlisten.controller;


import com.city.justlisten.entity.ListSong;
import com.city.justlisten.service.ListSongService;
import com.city.justlisten.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 歌单的歌曲管理controller
 */
@RestController
@RequestMapping("/listSong")
public class ListSongController {

    @Autowired
    private ListSongService listSongService;

    /**
     * 给歌单添加歌曲
     */
    @PostMapping("/add")
    public Result addListSong(HttpServletRequest request){
        //获取前端传来的参数
        String songId = request.getParameter("songId").trim();
        String songListId = request.getParameter("songListId").trim();
        ListSong listSong = new ListSong();
        listSong.setSongId(Integer.parseInt(songId));
        listSong.setSongListId(Integer.parseInt(songListId));
        boolean flag = listSongService.insert(listSong);
        if(flag){
            return Result.ok();
        }
        return Result.error();

    }

    /**
     * 根据歌单id查询歌曲
     */
    @GetMapping("/detail")
    public Result detail(HttpServletRequest request){
        String songListId = request.getParameter("songListId");
        List<ListSong> listSongs = listSongService.listSongOfSongListId(Integer.parseInt(songListId));
        return Result.ok().data("listSongs",listSongs);
    }

    /**
     * 删除歌单里的歌曲
     */
    @GetMapping("/delete")
    public Result delete(HttpServletRequest request){
        String songId = request.getParameter("songId").trim();
        String songListId = request.getParameter("songListId").trim();
        boolean flag = listSongService.deleteBySongIdAndSongListId(Integer.parseInt(songId),Integer.parseInt(songListId));
        if (flag){
            return Result.ok();
        }
        return Result.error();
    }

}